package satirGame;

import java.io.*;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class SituationEvent extends Event {
	private static String qType="Situation";
	private Performance SituationPerform;
	private int totalPlayer;
	private int countAnswered;
	
	public SituationEvent(int totalP) { 
        super(); 
        totalPlayer=totalP;
    } 
	
	public void loadQuestion(JFrame gameFrame) throws FileNotFoundException {
		SituationPerform=new Performance();
		countAnswered=0;
		String strFromFile = null;
		int count = 0;
		try {
			FileReader fr=new FileReader("2.txt");
			BufferedReader bfr=new BufferedReader(fr);
			 //read each question and stop at new question not yet used
			while ((strFromFile=bfr.readLine())!=null && count<newQuestionIndex) {
				count++;
			}
			newQuestionIndex++; //index of new question point to next line of the database
			
			//set question and option for Event type 2
			String[] SituationItem = strFromFile.split(";");
			setEventquestion(SituationItem[0]);
			options = new Object[SituationItem.length-1];
			for (int i = 1; i < SituationItem.length; i++) {
				options[i-1]=SituationItem[i];
			}
			
			for(Object name:options){
				System.out.println(name);
			}
			
			System.out.println((count+1)+" lines read");
			if((strFromFile=bfr.readLine())==null){
				newQuestionIndex=0;
			}
			
        }catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("File not found");//if file not found
        }catch(IOException e) { 
            e.printStackTrace(); 
        }
        
        for (int i = 0; i < totalPlayer; i++) {
        	QuestionDialog(gameFrame);
        }
        
        setScore(SituationPerform.getPlayerScore());
        System.out.println(getScore());
    }
	
	
	public void QuestionDialog(JFrame gameFrame) throws FileNotFoundException{
		String eventQuestion;
		if (countAnswered==0) {
			eventQuestion =(getEventquestion()+"，請選擇你的想法");
		}else {
			eventQuestion =(getEventquestion()+"，請其他未選擇玩家按下你的想法");
		}
		
		
		int initscore = JOptionPane.showOptionDialog(gameFrame,
				eventQuestion,
				qType,
				JOptionPane.YES_NO_OPTION,
				JOptionPane.QUESTION_MESSAGE,
				null,     //do not use a custom Icon
				options,  //the titles of buttons
				options[0]				   
				); //default button title
		if (countAnswered==0) {
			SituationPerform.setCurPlayerChoice(initscore);
		}else{
			SituationPerform.setOtherPlayerChoice(initscore);
		}
		countAnswered++;
		System.out.println("Score: " + initscore + options[initscore]);
	}

	
}
